import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: home
  },
  {
    path: '/classfiy',
    name: 'classfiy',
    component: () => import(/* webpackChunkName: "classfiy" */ '../views/classfiy.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    meta:{
      needLogin:true
    },
    component: () => import(/* webpackChunkName: "cart" */ '../views/cart.vue')
  },
  {
    path: '/myself',
    name: 'myself',
    meta:{
      needLogin:true
    },
    component: () => import(/* webpackChunkName: "myself" */ '../views/myself.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import(/* webpackChunkName: "detail" */ '../views/detail.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

import $store from '../store/index'

router.beforeEach((to,from,next)=>{
  if(to.meta.needLogin){
    if($store.state.user.username){
      next()
    }else{
      $store.commit('showLoginChange',true)
      router.push('/login')
    }
  }else{
    next()
  }
})

export default router
